﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using BLL;

namespace PhongMachTu.GUI
{
    public partial class frmQuyDinh : Form
    {
        public frmQuyDinh()
        {
            InitializeComponent();
        }
        ThamSoBLL bll = new ThamSoBLL();
        int thamSO = 1;
        private void frmQuyDinh_Load(object sender, EventArgs e)
        {
            if (bll.LayDSThamSo().Count > 0)
            {

                foreach (var item in bll.LayDSThamSo())
                {
                    thamSO = item.ThamSoID;
                    txtSoBenhNhanTD.Text = item.SoBenhNhanToiDa.ToString();
                    txtLoaiBenh.Text = item.SoLuongLoaiBenh.ToString();
                    txtDonVi.Text = item.SoLuongDonVi.ToString();
                    txtTienKham.Text = item.TienKham.ToString();
                    txtCachDung.Text = item.SoLuongCachDung.ToString();
                    txtThuoc.Text = item.SoLuongLoaiThuoc.ToString();
                }
            }

        }
        private int kiemTraSo(TextBox txt)
        {
            if (string.IsNullOrEmpty(txt.Text))
            {
                txt.Text = "0";
                return 0;
            }
            else
            {
                return int.Parse(txt.Text);
            }
        }
        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (bll.LayDSThamSo().Count - 1 < 0)
                {
                    THAMSO tsNew = new THAMSO();
                    tsNew.SoLuongLoaiThuoc = kiemTraSo(txtThuoc);
                    tsNew.SoLuongLoaiBenh = kiemTraSo(txtLoaiBenh);
                    tsNew.SoLuongDonVi = kiemTraSo(txtDonVi);
                    tsNew.SoLuongCachDung = kiemTraSo(txtTienKham);
                    tsNew.SoBenhNhanToiDa = kiemTraSo(txtSoBenhNhanTD);
                    tsNew.SoLuongCachDung = kiemTraSo(txtCachDung);
                    if ( bll.ThemThamSo(tsNew))
                    {
                        
                                   MsgBox.Success("Thêm quy định thành công !");
                    }
                    else
                    {
                        MsgBox.Error("Không thể thêm mới");
                    }

                }
                else
                {
                    THAMSO ts = new THAMSO();
                    ts.ThamSoID = thamSO;
                    ts.SoLuongLoaiThuoc = kiemTraSo(txtThuoc);
                    ts.SoLuongLoaiBenh = kiemTraSo(txtLoaiBenh);
                    ts.SoLuongDonVi = kiemTraSo(txtDonVi);
                    ts.SoBenhNhanToiDa = kiemTraSo(txtSoBenhNhanTD);
                    ts.SoLuongCachDung = kiemTraSo(txtCachDung);
                    ts.TienKham = kiemTraSo(txtTienKham);
                    if (bll.SuaThamSo(ts))
                    {
                        MsgBox.Success("Sửa quy định thành công !");
                    }
                    else
                    {
                        MsgBox.Error("Không thể sửa");
                    }
                }
            }
            catch (Exception r)
            {
                MsgBox.Error(r);

            }

        }

    }
}
